import { TagType, BusinessType, BusinessOptType } from "@/enums/sysEnum";
import { useUserStore } from "@/stores/modules/user";
import { useDataStore } from "@/stores/modules/data";
import httpRequest from "@/api/services";
import { reactive } from "vue";
import { UserAction } from "@/api/types/base";
import sysUrl from "@/api/services/sysUrl";
export const useUserAction = (businessType: string = BusinessType.SYS) => {
  const userInfo = useUserStore();
  const dataStore = useDataStore();

  const state = reactive({
    tableName: "userAction"
  });

  const addUserAction = async (
    businessId: string = "",
    optType: string = BusinessOptType.SYS,
    optInfo: string = "",
    tagType: TagType = TagType.INFO
  ) => {
    const action: UserAction = {
      businessId: businessId,
      businessType: businessType,
      type: tagType,
      optType: optType,
      optInfo: optInfo,
      optDate: new Date(),
      optUser: userInfo.userInfo._id,
      token: userInfo.token
    };
    await parseOptInfo(action);
    return httpRequest.post(sysUrl.BASE_SAVE + state.tableName, action);
  };

  const businessOptType = new Map();
  const parseOptInfo = async (action: UserAction) => {
    let dataList = await dataStore.findEnumByCsName("BUSINESS_OPT_TYPE");
    dataList.forEach(data => {
      businessOptType.set(data.value, data.name);
    });
    let optName = businessOptType.get(action.optType);
    action.optInfo = `${userInfo.userInfo.userName}-${optName}-${action.optInfo}`;
  };
  return {
    addUserAction
  };
};
